<template>
  <div class="add_all">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>管理员管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加管理员</el-breadcrumb-item>
    </el-breadcrumb>
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="账户名:" prop="salesName">
        <el-input v-model.number="ruleForm.salesName"></el-input>
      </el-form-item>
      <el-form-item label="密码:" prop="salesPass">
        <el-input type="password" v-model="ruleForm.salesPass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="年龄:" prop="age">
        <el-input v-model="ruleForm.age"></el-input>
      </el-form-item>
      <el-form-item label="性别:">
        <el-radio v-model="ruleForm.gender" label="男">男</el-radio>
        <el-radio v-model="ruleForm.gender" label="女">女</el-radio>
      </el-form-item>
      <el-form-item label="头像:">
        <el-avatar icon="el-icon-user-solid"></el-avatar>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
const { mapState, mapActions, mapMutations } = createNamespacedHelpers(
  "salesperson"
);
export default {
  data() {
    //验证
    var checkName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("用户名不能为空"));
      }
      callback();
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      }
      callback();
    };
    return {
      rules: {
        salesPass: [{ validator: validatePass, trigger: "blur" }],
        salesName: [{ validator: checkName, trigger: "blur" }],
        age: [
          { message: "请输入年龄", trigger: "blur" },
          {
            min: 1,
            max: 3,
            message: "长度在 1 到 3 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  computed: {
    ...mapState(["ruleForm"]),
  },
  methods: {
    ...mapActions(["addSalesPerson"]),
    ...mapMutations(["reset"]),
    async submitForm(formName) {
      const data = await this.addSalesPerson();
      if (data.status) {
        alert("销售员" + data.data.salesName + "添加成功！");
      }
    },
    resetForm(formName) {
      this.reset();
    },
  },
};
</script>

<style lang='scss' scoped>
@import "./css/addSalesPerson.scss";
</style>